home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2000 March / maximum-cd-2000-03.iso / Quake3 Game Source / Q3AGameSource.exe / Main / q_math.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-01-18  |  28.7 KB  |  1,270 lines

  1. // Copyright (C) 1999-2000 Id Software, Inc.
  2. //
  3. // q_math.c -- stateless support routines that are included in each code module
  4. #include "q_shared.h"
  5.  
  6.  
  7. vec3_t    vec3_origin = {0,0,0};
  8. vec3_t    axisDefault[3] = { { 1, 0, 0 }, { 0, 1, 0 }, { 0, 0, 1 } };
  9.  
  10.  
  11. vec4_t        colorBlack    = {0, 0, 0, 1};
  12. vec4_t        colorRed    = {1, 0, 0, 1};
  13. vec4_t        colorGreen    = {0, 1, 0, 1};
  14. vec4_t        colorBlue    = {0, 0, 1, 1};
  15. vec4_t        colorYellow    = {1, 1, 0, 1};
  16. vec4_t        colorMagenta= {1, 0, 1, 1};
  17. vec4_t        colorCyan    = {0, 1, 1, 1};
  18. vec4_t        colorWhite    = {1, 1, 1, 1};
  19. vec4_t        colorLtGrey    = {0.75, 0.75, 0.75, 1};
  20. vec4_t        colorMdGrey    = {0.5, 0.5, 0.5, 1};
  21. vec4_t        colorDkGrey    = {0.25, 0.25, 0.25, 1};
  22.  
  23. vec4_t    g_color_table[8] =
  24.     {
  25.     {0.0, 0.0, 0.0, 1.0},
  26.     {1.0, 0.0, 0.0, 1.0},
  27.     {0.0, 1.0, 0.0, 1.0},
  28.     {1.0, 1.0, 0.0, 1.0},
  29.     {0.0, 0.0, 1.0, 1.0},
  30.     {0.0, 1.0, 1.0, 1.0},
  31.     {1.0, 0.0, 1.0, 1.0},
  32.     {1.0, 1.0, 1.0, 1.0},
  33.     };
  34.  
  35.  
  36. vec3_t    bytedirs[NUMVERTEXNORMALS] =
  37. {
  38. {-0.525731, 0.000000, 0.850651}, {-0.442863, 0.238856, 0.864188}, 
  39. {-0.295242, 0.000000, 0.955423}, {-0.309017, 0.500000, 0.809017}, 
  40. {-0.162460, 0.262866, 0.951056}, {0.000000, 0.000000, 1.000000}, 
  41. {0.000000, 0.850651, 0.525731}, {-0.147621, 0.716567, 0.681718}, 
  42. {0.147621, 0.716567, 0.681718}, {0.000000, 0.525731, 0.850651}, 
  43. {0.309017, 0.500000, 0.809017}, {0.525731, 0.000000, 0.850651}, 
  44. {0.295242, 0.000000, 0.955423}, {0.442863, 0.238856, 0.864188}, 
  45. {0.162460, 0.262866, 0.951056}, {-0.681718, 0.147621, 0.716567}, 
  46. {-0.809017, 0.309017, 0.500000},{-0.587785, 0.425325, 0.688191}, 
  47. {-0.850651, 0.525731, 0.000000},{-0.864188, 0.442863, 0.238856}, 
  48. {-0.716567, 0.681718, 0.147621},{-0.688191, 0.587785, 0.425325}, 
  49. {-0.500000, 0.809017, 0.309017}, {-0.238856, 0.864188, 0.442863}, 
  50. {-0.425325, 0.688191, 0.587785}, {-0.716567, 0.681718, -0.147621}, 
  51. {-0.500000, 0.809017, -0.309017}, {-0.525731, 0.850651, 0.000000}, 
  52. {0.000000, 0.850651, -0.525731}, {-0.238856, 0.864188, -0.442863}, 
  53. {0.000000, 0.955423, -0.295242}, {-0.262866, 0.951056, -0.162460}, 
  54. {0.000000, 1.000000, 0.000000}, {0.000000, 0.955423, 0.295242}, 
  55. {-0.262866, 0.951056, 0.162460}, {0.238856, 0.864188, 0.442863}, 
  56. {0.262866, 0.951056, 0.162460}, {0.500000, 0.809017, 0.309017}, 
  57. {0.238856, 0.864188, -0.442863},{0.262866, 0.951056, -0.162460}, 
  58. {0.500000, 0.809017, -0.309017},{0.850651, 0.525731, 0.000000}, 
  59. {0.716567, 0.681718, 0.147621}, {0.716567, 0.681718, -0.147621}, 
  60. {0.525731, 0.850651, 0.000000}, {0.425325, 0.688191, 0.587785}, 
  61. {0.864188, 0.442863, 0.238856}, {0.688191, 0.587785, 0.425325}, 
  62. {0.809017, 0.309017, 0.500000}, {0.681718, 0.147621, 0.716567}, 
  63. {0.587785, 0.425325, 0.688191}, {0.955423, 0.295242, 0.000000}, 
  64. {1.000000, 0.000000, 0.000000}, {0.951056, 0.162460, 0.262866}, 
  65. {0.850651, -0.525731, 0.000000},{0.955423, -0.295242, 0.000000}, 
  66. {0.864188, -0.442863, 0.238856}, {0.951056, -0.162460, 0.262866}, 
  67. {0.809017, -0.309017, 0.500000}, {0.681718, -0.147621, 0.716567}, 
  68. {0.850651, 0.000000, 0.525731}, {0.864188, 0.442863, -0.238856}, 
  69. {0.809017, 0.309017, -0.500000}, {0.951056, 0.162460, -0.262866}, 
  70. {0.525731, 0.000000, -0.850651}, {0.681718, 0.147621, -0.716567}, 
  71. {0.681718, -0.147621, -0.716567},{0.850651, 0.000000, -0.525731}, 
  72. {0.809017, -0.309017, -0.500000}, {0.864188, -0.442863, -0.238856}, 
  73. {0.951056, -0.162460, -0.262866}, {0.147621, 0.716567, -0.681718}, 
  74. {0.309017, 0.500000, -0.809017}, {0.425325, 0.688191, -0.587785}, 
  75. {0.442863, 0.238856, -0.864188}, {0.587785, 0.425325, -0.688191}, 
  76. {0.688191, 0.587785, -0.425325}, {-0.147621, 0.716567, -0.681718}, 
  77. {-0.309017, 0.500000, -0.809017}, {0.000000, 0.525731, -0.850651}, 
  78. {-0.525731, 0.000000, -0.850651}, {-0.442863, 0.238856, -0.864188}, 
  79. {-0.295242, 0.000000, -0.955423}, {-0.162460, 0.262866, -0.951056}, 
  80. {0.000000, 0.000000, -1.000000}, {0.295242, 0.000000, -0.955423}, 
  81. {0.162460, 0.262866, -0.951056}, {-0.442863, -0.238856, -0.864188}, 
  82. {-0.309017, -0.500000, -0.809017}, {-0.162460, -0.262866, -0.951056}, 
  83. {0.000000, -0.850651, -0.525731}, {-0.147621, -0.716567, -0.681718}, 
  84. {0.147621, -0.716567, -0.681718}, {0.000000, -0.525731, -0.850651}, 
  85. {0.309017, -0.500000, -0.809017}, {0.442863, -0.238856, -0.864188}, 
  86. {0.162460, -0.262866, -0.951056}, {0.238856, -0.864188, -0.442863}, 
  87. {0.500000, -0.809017, -0.309017}, {0.425325, -0.688191, -0.587785}, 
  88. {0.716567, -0.681718, -0.147621}, {0.688191, -0.587785, -0.425325}, 
  89. {0.587785, -0.425325, -0.688191}, {0.000000, -0.955423, -0.295242}, 
  90. {0.000000, -1.000000, 0.000000}, {0.262866, -0.951056, -0.162460}, 
  91. {0.000000, -0.850651, 0.525731}, {0.000000, -0.955423, 0.295242}, 
  92. {0.238856, -0.864188, 0.442863}, {0.262866, -0.951056, 0.162460}, 
  93. {0.500000, -0.809017, 0.309017}, {0.716567, -0.681718, 0.147621}, 
  94. {0.525731, -0.850651, 0.000000}, {-0.238856, -0.864188, -0.442863}, 
  95. {-0.500000, -0.809017, -0.309017}, {-0.262866, -0.951056, -0.162460}, 
  96. {-0.850651, -0.525731, 0.000000}, {-0.716567, -0.681718, -0.147621}, 
  97. {-0.716567, -0.681718, 0.147621}, {-0.525731, -0.850651, 0.000000}, 
  98. {-0.500000, -0.809017, 0.309017}, {-0.238856, -0.864188, 0.442863}, 
  99. {-0.262866, -0.951056, 0.162460}, {-0.864188, -0.442863, 0.238856}, 
  100. {-0.809017, -0.309017, 0.500000}, {-0.688191, -0.587785, 0.425325}, 
  101. {-0.681718, -0.147621, 0.716567}, {-0.442863, -0.238856, 0.864188}, 
  102. {-0.587785, -0.425325, 0.688191}, {-0.309017, -0.500000, 0.809017}, 
  103. {-0.147621, -0.716567, 0.681718}, {-0.425325, -0.688191, 0.587785}, 
  104. {-0.162460, -0.262866, 0.951056}, {0.442863, -0.238856, 0.864188}, 
  105. {0.162460, -0.262866, 0.951056}, {0.309017, -0.500000, 0.809017}, 
  106. {0.147621, -0.716567, 0.681718}, {0.000000, -0.525731, 0.850651}, 
  107. {0.425325, -0.688191, 0.587785}, {0.587785, -0.425325, 0.688191}, 
  108. {0.688191, -0.587785, 0.425325}, {-0.955423, 0.295242, 0.000000}, 
  109. {-0.951056, 0.162460, 0.262866}, {-1.000000, 0.000000, 0.000000}, 
  110. {-0.850651, 0.000000, 0.525731}, {-0.955423, -0.295242, 0.000000}, 
  111. {-0.951056, -0.162460, 0.262866}, {-0.864188, 0.442863, -0.238856}, 
  112. {-0.951056, 0.162460, -0.262866}, {-0.809017, 0.309017, -0.500000}, 
  113. {-0.864188, -0.442863, -0.238856}, {-0.951056, -0.162460, -0.262866}, 
  114. {-0.809017, -0.309017, -0.500000}, {-0.681718, 0.147621, -0.716567}, 
  115. {-0.681718, -0.147621, -0.716567}, {-0.850651, 0.000000, -0.525731}, 
  116. {-0.688191, 0.587785, -0.425325}, {-0.587785, 0.425325, -0.688191}, 
  117. {-0.425325, 0.688191, -0.587785}, {-0.425325, -0.688191, -0.587785}, 
  118. {-0.587785, -0.425325, -0.688191}, {-0.688191, -0.587785, -0.425325}
  119. };
  120.  
  121. //==============================================================
  122.  
  123. int        Q_rand( int *seed ) {
  124.     *seed = (69069 * *seed + 1);
  125.     return *seed;
  126. }
  127.  
  128. float    Q_random( int *seed ) {
  129.     return ( Q_rand( seed ) & 0xffff ) / (float)0x10000;
  130. }
  131.  
  132. float    Q_crandom( int *seed ) {
  133.     return 2.0 * ( Q_random( seed ) - 0.5 );
  134. }
  135.  
  136.  
  137. //=======================================================
  138.  
  139. signed char ClampChar( int i ) {
  140.     if ( i < -128 ) {
  141.         return -128;
  142.     }
  143.     if ( i > 127 ) {
  144.         return 127;
  145.     }
  146.     return i;
  147. }
  148.  
  149. signed short ClampShort( int i ) {
  150.     if ( i < (short)0x8000 ) {
  151.         return (short)0x8000;
  152.     }
  153.     if ( i > 0x7fff ) {
  154.         return 0x7fff;
  155.     }
  156.     return i;
  157. }
  158.  
  159.  
  160. // this isn't a real cheap function to call!
  161. int DirToByte( vec3_t dir ) {
  162.     int        i, best;
  163.     float    d, bestd;
  164.  
  165.     if ( !dir ) {
  166.         return 0;
  167.     }
  168.  
  169.     bestd = 0;
  170.     best = 0;
  171.     for (i=0 ; i<NUMVERTEXNORMALS ; i++)
  172.     {
  173.         d = DotProduct (dir, bytedirs[i]);
  174.         if (d > bestd)
  175.         {
  176.             bestd = d;
  177.             best = i;
  178.         }
  179.     }
  180.  
  181.     return best;
  182. }
  183.  
  184. void ByteToDir( int b, vec3_t dir ) {
  185.     if ( b < 0 || b >= NUMVERTEXNORMALS ) {
  186.         VectorCopy( vec3_origin, dir );
  187.         return;
  188.     }
  189.     VectorCopy (bytedirs[b], dir);
  190. }
  191.  
  192.  
  193. unsigned ColorBytes3 (float r, float g, float b) {
  194.     unsigned    i;
  195.  
  196.     ( (byte *)&i )[0] = r * 255;
  197.     ( (byte *)&i )[1] = g * 255;
  198.     ( (byte *)&i )[2] = b * 255;
  199.  
  200.     return i;
  201. }
  202.  
  203. unsigned ColorBytes4 (float r, float g, float b, float a) {
  204.     unsigned    i;
  205.  
  206.     ( (byte *)&i )[0] = r * 255;
  207.     ( (byte *)&i )[1] = g * 255;
  208.     ( (byte *)&i )[2] = b * 255;
  209.     ( (byte *)&i )[3] = a * 255;
  210.  
  211.     return i;
  212. }
  213.  
  214. float NormalizeColor( const vec3_t in, vec3_t out ) {
  215.     float    max;
  216.     
  217.     max = in[0];
  218.     if ( in[1] > max ) {
  219.         max = in[1];
  220.     }
  221.     if ( in[2] > max ) {
  222.         max = in[2];
  223.     }
  224.  
  225.     if ( !max ) {
  226.         VectorClear( out );
  227.     } else {
  228.         out[0] = in[0] / max;
  229.         out[1] = in[1] / max;
  230.         out[2] = in[2] / max;
  231.     }
  232.     return max;
  233. }
  234.  
  235.  
  236. //============================================================================
  237.  
  238. /*
  239. =====================
  240. PlaneFromPoints
  241.  
  242. Returns false if the triangle is degenrate.
  243. The normal will point out of the clock for clockwise ordered points
  244. =====================
  245. */
  246. qboolean PlaneFromPoints( vec4_t plane, const vec3_t a, const vec3_t b, const vec3_t c ) {
  247.     vec3_t    d1, d2;
  248.  
  249.     VectorSubtract( b, a, d1 );
  250.     VectorSubtract( c, a, d2 );
  251.     CrossProduct( d2, d1, plane );
  252.     if ( VectorNormalize( plane ) == 0 ) {
  253.         return qfalse;
  254.     }
  255.  
  256.     plane[3] = DotProduct( a, plane );
  257.     return qtrue;
  258. }
  259.  
  260. /*
  261. ===============
  262. RotatePointAroundVector
  263.  
  264. This is not implemented very well...
  265. ===============
  266. */
  267. void RotatePointAroundVector( vec3_t dst, const vec3_t dir, const vec3_t point,
  268.                              float degrees ) {
  269.     float    m[3][3];
  270.     float    im[3][3];
  271.     float    zrot[3][3];
  272.     float    tmpmat[3][3];
  273.     float    rot[3][3];
  274.     int    i;
  275.     vec3_t vr, vup, vf;
  276.     float    rad;
  277.  
  278.     vf[0] = dir[0];
  279.     vf[1] = dir[1];
  280.     vf[2] = dir[2];
  281.  
  282.     PerpendicularVector( vr, dir );
  283.     CrossProduct( vr, vf, vup );
  284.  
  285.     m[0][0] = vr[0];
  286.     m[1][0] = vr[1];
  287.     m[2][0] = vr[2];
  288.  
  289.     m[0][1] = vup[0];
  290.     m[1][1] = vup[1];
  291.     m[2][1] = vup[2];
  292.  
  293.     m[0][2] = vf[0];
  294.     m[1][2] = vf[1];
  295.     m[2][2] = vf[2];
  296.  
  297.     memcpy( im, m, sizeof( im ) );
  298.  
  299.     im[0][1] = m[1][0];
  300.     im[0][2] = m[2][0];
  301.     im[1][0] = m[0][1];
  302.     im[1][2] = m[2][1];
  303.     im[2][0] = m[0][2];
  304.     im[2][1] = m[1][2];
  305.  
  306.     memset( zrot, 0, sizeof( zrot ) );
  307.     zrot[0][0] = zrot[1][1] = zrot[2][2] = 1.0F;
  308.  
  309.     rad = DEG2RAD( degrees );
  310.     zrot[0][0] = cos( rad );
  311.     zrot[0][1] = sin( rad );
  312.     zrot[1][0] = -sin( rad );
  313.     zrot[1][1] = cos( rad );
  314.  
  315.     MatrixMultiply( m, zrot, tmpmat );
  316.     MatrixMultiply( tmpmat, im, rot );
  317.  
  318.     for ( i = 0; i < 3; i++ ) {
  319.         dst[i] = rot[i][0] * point[0] + rot[i][1] * point[1] + rot[i][2] * point[2];
  320.     }
  321. }
  322.  
  323. /*
  324. ===============
  325. RotateAroundDirection
  326. ===============
  327. */
  328. void RotateAroundDirection( vec3_t axis[3], float yaw ) {
  329.  
  330.     // create an arbitrary axis[1] 
  331.     PerpendicularVector( axis[1], axis[0] );
  332.  
  333.     // rotate it around axis[0] by yaw
  334.     if ( yaw ) {
  335.         vec3_t    temp;
  336.  
  337.         VectorCopy( axis[1], temp );
  338.         RotatePointAroundVector( axis[1], axis[0], temp, yaw );
  339.     }
  340.  
  341.     // cross to get axis[2]
  342.     CrossProduct( axis[0], axis[1], axis[2] );
  343. }
  344.  
  345.  
  346.  
  347. void vectoangles( const vec3_t value1, vec3_t angles ) {
  348.     float    forward;
  349.     float    yaw, pitch;
  350.     
  351.     if ( value1[1] == 0 && value1[0] == 0 ) {
  352.         yaw = 0;
  353.         if ( value1[2] > 0 ) {
  354.             pitch = 90;
  355.         }
  356.         else {
  357.             pitch = 270;
  358.         }
  359.     }
  360.     else {
  361.         if ( value1[0] ) {
  362.             yaw = ( atan2 ( value1[1], value1[0] ) * 180 / M_PI );
  363.         }
  364.         else if ( value1[1] > 0 ) {
  365.             yaw = 90;
  366.         }
  367.         else {
  368.             yaw = 270;
  369.         }
  370.         if ( yaw < 0 ) {
  371.             yaw += 360;
  372.         }
  373.  
  374.         forward = sqrt ( value1[0]*value1[0] + value1[1]*value1[1] );
  375.         pitch = ( atan2(value1[2], forward) * 180 / M_PI );
  376.         if ( pitch < 0 ) {
  377.             pitch += 360;
  378.         }
  379.     }
  380.  
  381.     angles[PITCH] = -pitch;
  382.     angles[YAW] = yaw;
  383.     angles[ROLL] = 0;
  384. }
  385.  
  386.  
  387. /*
  388. =================
  389. AnglesToAxis
  390. =================
  391. */
  392. void AnglesToAxis( const vec3_t angles, vec3_t axis[3] ) {
  393.     vec3_t    right;
  394.  
  395.     // angle vectors returns "right" instead of "y axis"
  396.     AngleVectors( angles, axis[0], right, axis[2] );
  397.     VectorSubtract( vec3_origin, right, axis[1] );
  398. }
  399.  
  400. void AxisClear( vec3_t axis[3] ) {
  401.     axis[0][0] = 1;
  402.     axis[0][1] = 0;
  403.     axis[0][2] = 0;
  404.     axis[1][0] = 0;
  405.     axis[1][1] = 1;
  406.     axis[1][2] = 0;
  407.     axis[2][0] = 0;
  408.     axis[2][1] = 0;
  409.     axis[2][2] = 1;
  410. }
  411.  
  412. void AxisCopy( vec3_t in[3], vec3_t out[3] ) {
  413.     VectorCopy( in[0], out[0] );
  414.     VectorCopy( in[1], out[1] );
  415.     VectorCopy( in[2], out[2] );
  416. }
  417.  
  418. void ProjectPointOnPlane( vec3_t dst, const vec3_t p, const vec3_t normal )
  419. {
  420.     float d;
  421.     vec3_t n;
  422.     float inv_denom;
  423.  
  424.     inv_denom = 1.0F / DotProduct( normal, normal );
  425.  
  426.     d = DotProduct( normal, p ) * inv_denom;
  427.  
  428.     n[0] = normal[0] * inv_denom;
  429.     n[1] = normal[1] * inv_denom;
  430.     n[2] = normal[2] * inv_denom;
  431.  
  432.     dst[0] = p[0] - d * n[0];
  433.     dst[1] = p[1] - d * n[1];
  434.     dst[2] = p[2] - d * n[2];
  435. }
  436.  
  437. /*
  438. ================
  439. MakeNormalVectors
  440.  
  441. Given a normalized forward vector, create two
  442. other perpendicular vectors
  443. ================
  444. */
  445. void MakeNormalVectors( const vec3_t forward, vec3_t right, vec3_t up) {
  446.     float        d;
  447.  
  448.     // this rotate and negate guarantees a vector
  449.     // not colinear with the original
  450.     right[1] = -forward[0];
  451.     right[2] = forward[1];
  452.     right[0] = forward[2];
  453.  
  454.     d = DotProduct (right, forward);
  455.     VectorMA (right, -d, forward, right);
  456.     VectorNormalize (right);
  457.     CrossProduct (right, forward, up);
  458. }
  459.  
  460.  
  461. void VectorRotate( vec3_t in, vec3_t matrix[3], vec3_t out )
  462. {
  463.     out[0] = DotProduct( in, matrix[0] );
  464.     out[1] = DotProduct( in, matrix[1] );
  465.     out[2] = DotProduct( in, matrix[2] );
  466. }
  467.  
  468. //============================================================================
  469.  
  470. /*
  471. ** float q_rsqrt( float number )
  472. */
  473. float Q_rsqrt( float number )
  474. {
  475.     long i;
  476.     float x2, y;
  477.     const float threehalfs = 1.5F;
  478.  
  479.     x2 = number * 0.5F;
  480.     y  = number;
  481.     i  = * ( long * ) &y;                        // evil floating point bit level hacking
  482.     i  = 0x5f3759df - ( i >> 1 );               // what the fuck?
  483.     y  = * ( float * ) &i;
  484.     y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
  485. //    y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed
  486.  
  487.     return y;
  488. }
  489.  
  490. float Q_fabs( float f ) {
  491.     int tmp = * ( int * ) &f;
  492.     tmp &= 0x7FFFFFFF;
  493.     return * ( float * ) &tmp;
  494. }
  495.  
  496. //============================================================
  497.  
  498. /*
  499. ===============
  500. LerpAngle
  501.  
  502. ===============
  503. */
  504. float LerpAngle (float from, float to, float frac) {
  505.     float    a;
  506.  
  507.     if ( to - from > 180 ) {
  508.         to -= 360;
  509.     }
  510.     if ( to - from < -180 ) {
  511.         to += 360;
  512.     }
  513.     a = from + frac * (to - from);
  514.  
  515.     return a;
  516. }
  517.  
  518.  
  519. /*
  520. =================
  521. AngleSubtract
  522.  
  523. Always returns a value from -180 to 180
  524. =================
  525. */
  526. float    AngleSubtract( float a1, float a2 ) {
  527.     float    a;
  528.  
  529.     a = a1 - a2;
  530.     while ( a > 180 ) {
  531.         a -= 360;
  532.     }
  533.     while ( a < -180 ) {
  534.         a += 360;
  535.     }
  536.     return a;
  537. }
  538.  
  539.  
  540. void AnglesSubtract( vec3_t v1, vec3_t v2, vec3_t v3 ) {
  541.     v3[0] = AngleSubtract( v1[0], v2[0] );
  542.     v3[1] = AngleSubtract( v1[1], v2[1] );
  543.     v3[2] = AngleSubtract( v1[2], v2[2] );
  544. }
  545.  
  546.  
  547. float    AngleMod(float a) {
  548.     a = (360.0/65536) * ((int)(a*(65536/360.0)) & 65535);
  549.     return a;
  550. }
  551.  
  552.  
  553. /*
  554. =================
  555. AngleNormalize360
  556.  
  557. returns angle normalized to the range [0 <= angle < 360]
  558. =================
  559. */
  560. float AngleNormalize360 ( float angle ) {
  561.     return (360.0 / 65536) * ((int)(angle * (65536 / 360.0)) & 65535);
  562. }
  563.  
  564.  
  565. /*
  566. =================
  567. AngleNormalize180
  568.  
  569. returns angle normalized to the range [-180 < angle <= 180]
  570. =================
  571. */
  572. float AngleNormalize180 ( float angle ) {
  573.     angle = AngleNormalize360( angle );
  574.     if ( angle > 180.0 ) {
  575.         angle -= 360.0;
  576.     }
  577.     return angle;
  578. }
  579.  
  580.  
  581. /*
  582. =================
  583. AngleDelta
  584.  
  585. returns the normalized delta from angle1 to angle2
  586. =================
  587. */
  588. float AngleDelta ( float angle1, float angle2 ) {
  589.     return AngleNormalize180( angle1 - angle2 );
  590. }
  591.  
  592.  
  593. //============================================================
  594.  
  595.  
  596. /*
  597. =================
  598. SetPlaneSignbits
  599. =================
  600. */
  601. void SetPlaneSignbits (cplane_t *out) {
  602.     int    bits, j;
  603.  
  604.     // for fast box on planeside test
  605.     bits = 0;
  606.     for (j=0 ; j<3 ; j++) {
  607.         if (out->normal[j] < 0) {
  608.             bits |= 1<<j;
  609.         }
  610.     }
  611.     out->signbits = bits;
  612. }
  613.  
  614.  
  615. /*
  616. ==================
  617. BoxOnPlaneSide
  618.  
  619. Returns 1, 2, or 1 + 2
  620.  
  621. // this is the slow, general version
  622. int BoxOnPlaneSide2 (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  623. {
  624.     int        i;
  625.     float    dist1, dist2;
  626.     int        sides;
  627.     vec3_t    corners[2];
  628.  
  629.     for (i=0 ; i<3 ; i++)
  630.     {
  631.         if (p->normal[i] < 0)
  632.         {
  633.             corners[0][i] = emins[i];
  634.             corners[1][i] = emaxs[i];
  635.         }
  636.         else
  637.         {
  638.             corners[1][i] = emins[i];
  639.             corners[0][i] = emaxs[i];
  640.         }
  641.     }
  642.     dist1 = DotProduct (p->normal, corners[0]) - p->dist;
  643.     dist2 = DotProduct (p->normal, corners[1]) - p->dist;
  644.     sides = 0;
  645.     if (dist1 >= 0)
  646.         sides = 1;
  647.     if (dist2 < 0)
  648.         sides |= 2;
  649.  
  650.     return sides;
  651. }
  652.  
  653. ==================
  654. */
  655. #if !(defined __linux__ && defined __i386__ && !defined C_ONLY)
  656. #if defined __LCC__ || defined C_ONLY || !id386
  657.  
  658. int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  659. {
  660.     float    dist1, dist2;
  661.     int        sides;
  662.  
  663. // fast axial cases
  664.     if (p->type < 3)
  665.     {
  666.         if (p->dist <= emins[p->type])
  667.             return 1;
  668.         if (p->dist >= emaxs[p->type])
  669.             return 2;
  670.         return 3;
  671.     }
  672.  
  673. // general case
  674.     switch (p->signbits)
  675.     {
  676.     case 0:
  677.         dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  678.         dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  679.         break;
  680.     case 1:
  681.         dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  682.         dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  683.         break;
  684.     case 2:
  685.         dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  686.         dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  687.         break;
  688.     case 3:
  689.         dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  690.         dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  691.         break;
  692.     case 4:
  693.         dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  694.         dist2 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  695.         break;
  696.     case 5:
  697.         dist1 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emins[2];
  698.         dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emaxs[2];
  699.         break;
  700.     case 6:
  701.         dist1 = p->normal[0]*emaxs[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  702.         dist2 = p->normal[0]*emins[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  703.         break;
  704.     case 7:
  705.         dist1 = p->normal[0]*emins[0] + p->normal[1]*emins[1] + p->normal[2]*emins[2];
  706.         dist2 = p->normal[0]*emaxs[0] + p->normal[1]*emaxs[1] + p->normal[2]*emaxs[2];
  707.         break;
  708.     default:
  709.         dist1 = dist2 = 0;        // shut up compiler
  710.         break;
  711.     }
  712.  
  713.     sides = 0;
  714.     if (dist1 >= p->dist)
  715.         sides = 1;
  716.     if (dist2 < p->dist)
  717.         sides |= 2;
  718.  
  719.     return sides;
  720. }
  721. #else
  722. #pragma warning( disable: 4035 )
  723.  
  724. __declspec( naked ) int BoxOnPlaneSide (vec3_t emins, vec3_t emaxs, struct cplane_s *p)
  725. {
  726.     static int bops_initialized;
  727.     static int Ljmptab[8];
  728.  
  729.     __asm {
  730.  
  731.         push ebx
  732.             
  733.         cmp bops_initialized, 1
  734.         je  initialized
  735.         mov bops_initialized, 1
  736.         
  737.         mov Ljmptab[0*4], offset Lcase0
  738.         mov Ljmptab[1*4], offset Lcase1
  739.         mov Ljmptab[2*4], offset Lcase2
  740.         mov Ljmptab[3*4], offset Lcase3
  741.         mov Ljmptab[4*4], offset Lcase4
  742.         mov Ljmptab[5*4], offset Lcase5
  743.         mov Ljmptab[6*4], offset Lcase6
  744.         mov Ljmptab[7*4], offset Lcase7
  745.             
  746. initialized:
  747.  
  748.         mov edx,dword ptr[4+12+esp]
  749.         mov ecx,dword ptr[4+4+esp]
  750.         xor eax,eax
  751.         mov ebx,dword ptr[4+8+esp]
  752.         mov al,byte ptr[17+edx]
  753.         cmp al,8
  754.         jge Lerror
  755.         fld dword ptr[0+edx]
  756.         fld st(0)
  757.         jmp dword ptr[Ljmptab+eax*4]
  758. Lcase0:
  759.         fmul dword ptr[ebx]
  760.         fld dword ptr[0+4+edx]
  761.         fxch st(2)
  762.         fmul dword ptr[ecx]
  763.         fxch st(2)
  764.         fld st(0)
  765.         fmul dword ptr[4+ebx]
  766.         fld dword ptr[0+8+edx]
  767.         fxch st(2)
  768.         fmul dword ptr[4+ecx]
  769.         fxch st(2)
  770.         fld st(0)
  771.         fmul dword ptr[8+ebx]
  772.         fxch st(5)
  773.         faddp st(3),st(0)
  774.         fmul dword ptr[8+ecx]
  775.         fxch st(1)
  776.         faddp st(3),st(0)
  777.         fxch st(3)
  778.         faddp st(2),st(0)
  779.         jmp LSetSides
  780. Lcase1:
  781.         fmul dword ptr[ecx]
  782.         fld dword ptr[0+4+edx]
  783.         fxch st(2)
  784.         fmul dword ptr[ebx]
  785.         fxch st(2)
  786.         fld st(0)
  787.         fmul dword ptr[4+ebx]
  788.         fld dword ptr[0+8+edx]
  789.         fxch st(2)
  790.         fmul dword ptr[4+ecx]
  791.         fxch st(2)
  792.         fld st(0)
  793.         fmul dword ptr[8+ebx]
  794.         fxch st(5)
  795.         faddp st(3),st(0)
  796.         fmul dword ptr[8+ecx]
  797.         fxch st(1)
  798.         faddp st(3),st(0)
  799.         fxch st(3)
  800.         faddp st(2),st(0)
  801.         jmp LSetSides
  802. Lcase2:
  803.         fmul dword ptr[ebx]
  804.         fld dword ptr[0+4+edx]
  805.         fxch st(2)
  806.         fmul dword ptr[ecx]
  807.         fxch st(2)
  808.         fld st(0)
  809.         fmul dword ptr[4+ecx]
  810.         fld dword ptr[0+8+edx]
  811.         fxch st(2)
  812.         fmul dword ptr[4+ebx]
  813.         fxch st(2)
  814.         fld st(0)
  815.         fmul dword ptr[8+ebx]
  816.         fxch st(5)
  817.         faddp st(3),st(0)
  818.         fmul dword ptr[8+ecx]
  819.         fxch st(1)
  820.         faddp st(3),st(0)
  821.         fxch st(3)
  822.         faddp st(2),st(0)
  823.         jmp LSetSides
  824. Lcase3:
  825.         fmul dword ptr[ecx]
  826.         fld dword ptr[0+4+edx]
  827.         fxch st(2)
  828.         fmul dword ptr[ebx]
  829.         fxch st(2)
  830.         fld st(0)
  831.         fmul dword ptr[4+ecx]
  832.         fld dword ptr[0+8+edx]
  833.         fxch st(2)
  834.         fmul dword ptr[4+ebx]
  835.         fxch st(2)
  836.         fld st(0)
  837.         fmul dword ptr[8+ebx]
  838.         fxch st(5)
  839.         faddp st(3),st(0)
  840.         fmul dword ptr[8+ecx]
  841.         fxch st(1)
  842.         faddp st(3),st(0)
  843.         fxch st(3)
  844.         faddp st(2),st(0)
  845.         jmp LSetSides
  846. Lcase4:
  847.         fmul dword ptr[ebx]
  848.         fld dword ptr[0+4+edx]
  849.         fxch st(2)
  850.         fmul dword ptr[ecx]
  851.         fxch st(2)
  852.         fld st(0)
  853.         fmul dword ptr[4+ebx]
  854.         fld dword ptr[0+8+edx]
  855.         fxch st(2)
  856.         fmul dword ptr[4+ecx]
  857.         fxch st(2)
  858.         fld st(0)
  859.         fmul dword ptr[8+ecx]
  860.         fxch st(5)
  861.         faddp st(3),st(0)
  862.         fmul dword ptr[8+ebx]
  863.         fxch st(1)
  864.         faddp st(3),st(0)
  865.         fxch st(3)
  866.         faddp st(2),st(0)
  867.         jmp LSetSides
  868. Lcase5:
  869.         fmul dword ptr[ecx]
  870.         fld dword ptr[0+4+edx]
  871.         fxch st(2)
  872.         fmul dword ptr[ebx]
  873.         fxch st(2)
  874.         fld st(0)
  875.         fmul dword ptr[4+ebx]
  876.         fld dword ptr[0+8+edx]
  877.         fxch st(2)
  878.         fmul dword ptr[4+ecx]
  879.         fxch st(2)
  880.         fld st(0)
  881.         fmul dword ptr[8+ecx]
  882.         fxch st(5)
  883.         faddp st(3),st(0)
  884.         fmul dword ptr[8+ebx]
  885.         fxch st(1)
  886.         faddp st(3),st(0)
  887.         fxch st(3)
  888.         faddp st(2),st(0)
  889.         jmp LSetSides
  890. Lcase6:
  891.         fmul dword ptr[ebx]
  892.         fld dword ptr[0+4+edx]
  893.         fxch st(2)
  894.         fmul dword ptr[ecx]
  895.         fxch st(2)
  896.         fld st(0)
  897.         fmul dword ptr[4+ecx]
  898.         fld dword ptr[0+8+edx]
  899.         fxch st(2)
  900.         fmul dword ptr[4+ebx]
  901.         fxch st(2)
  902.         fld st(0)
  903.         fmul dword ptr[8+ecx]
  904.         fxch st(5)
  905.         faddp st(3),st(0)
  906.         fmul dword ptr[8+ebx]
  907.         fxch st(1)
  908.         faddp st(3),st(0)
  909.         fxch st(3)
  910.         faddp st(2),st(0)
  911.         jmp LSetSides
  912. Lcase7:
  913.         fmul dword ptr[ecx]
  914.         fld dword ptr[0+4+edx]
  915.         fxch st(2)
  916.         fmul dword ptr[ebx]
  917.         fxch st(2)
  918.         fld st(0)
  919.         fmul dword ptr[4+ecx]
  920.         fld dword ptr[0+8+edx]
  921.         fxch st(2)
  922.         fmul dword ptr[4+ebx]
  923.         fxch st(2)
  924.         fld st(0)
  925.         fmul dword ptr[8+ecx]
  926.         fxch st(5)
  927.         faddp st(3),st(0)
  928.         fmul dword ptr[8+ebx]
  929.         fxch st(1)
  930.         faddp st(3),st(0)
  931.         fxch st(3)
  932.         faddp st(2),st(0)
  933. LSetSides:
  934.         faddp st(2),st(0)
  935.         fcomp dword ptr[12+edx]
  936.         xor ecx,ecx
  937.         fnstsw ax
  938.         fcomp dword ptr[12+edx]
  939.         and ah,1
  940.         xor ah,1
  941.         add cl,ah
  942.         fnstsw ax
  943.         and ah,1
  944.         add ah,ah
  945.         add cl,ah
  946.         pop ebx
  947.         mov eax,ecx
  948.         ret
  949. Lerror:
  950.         int 3
  951.     }
  952. }
  953. #pragma warning( default: 4035 )
  954.  
  955. #endif
  956. #endif
  957.  
  958. /*
  959. =================
  960. RadiusFromBounds
  961. =================
  962. */
  963. float RadiusFromBounds( const vec3_t mins, const vec3_t maxs ) {
  964.     int        i;
  965.     vec3_t    corner;
  966.     float    a, b;
  967.  
  968.     for (i=0 ; i<3 ; i++) {
  969.         a = fabs( mins[i] );
  970.         b = fabs( maxs[i] );
  971.         corner[i] = a > b ? a : b;
  972.     }
  973.  
  974.     return VectorLength (corner);
  975. }
  976.  
  977.  
  978. void ClearBounds( vec3_t mins, vec3_t maxs ) {
  979.     mins[0] = mins[1] = mins[2] = 99999;
  980.     maxs[0] = maxs[1] = maxs[2] = -99999;
  981. }
  982.  
  983. void AddPointToBounds( const vec3_t v, vec3_t mins, vec3_t maxs ) {
  984.     if ( v[0] < mins[0] ) {
  985.         mins[0] = v[0];
  986.     }
  987.     if ( v[0] > maxs[0]) {
  988.         maxs[0] = v[0];
  989.     }
  990.  
  991.     if ( v[1] < mins[1] ) {
  992.         mins[1] = v[1];
  993.     }
  994.     if ( v[1] > maxs[1]) {
  995.         maxs[1] = v[1];
  996.     }
  997.  
  998.     if ( v[2] < mins[2] ) {
  999.         mins[2] = v[2];
  1000.     }
  1001.     if ( v[2] > maxs[2]) {
  1002.         maxs[2] = v[2];
  1003.     }
  1004. }
  1005.  
  1006.  
  1007. int VectorCompare( const vec3_t v1, const vec3_t v2 ) {
  1008.     if (v1[0] != v2[0] || v1[1] != v2[1] || v1[2] != v2[2]) {
  1009.         return 0;
  1010.     }
  1011.             
  1012.     return 1;
  1013. }
  1014.  
  1015.  
  1016. vec_t VectorNormalize( vec3_t v ) {
  1017.     float    length, ilength;
  1018.  
  1019.     length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  1020.     length = sqrt (length);
  1021.  
  1022.     if ( length ) {
  1023.         ilength = 1/length;
  1024.         v[0] *= ilength;
  1025.         v[1] *= ilength;
  1026.         v[2] *= ilength;
  1027.     }
  1028.         
  1029.     return length;
  1030. }
  1031.  
  1032. //
  1033. // fast vector normalize routine that does not check to make sure
  1034. // that length != 0, nor does it return length
  1035. //
  1036. void VectorNormalizeFast( vec3_t v )
  1037. {
  1038.     float ilength;
  1039.  
  1040.     ilength = Q_rsqrt( DotProduct( v, v ) );
  1041.  
  1042.     v[0] *= ilength;
  1043.     v[1] *= ilength;
  1044.     v[2] *= ilength;
  1045. }
  1046.  
  1047. vec_t VectorNormalize2( const vec3_t v, vec3_t out) {
  1048.     float    length, ilength;
  1049.  
  1050.     length = v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  1051.     length = sqrt (length);
  1052.  
  1053.     if (length)
  1054.     {
  1055.         ilength = 1/length;
  1056.         out[0] = v[0]*ilength;
  1057.         out[1] = v[1]*ilength;
  1058.         out[2] = v[2]*ilength;
  1059.     } else {
  1060.         VectorClear( out );
  1061.     }
  1062.         
  1063.     return length;
  1064.  
  1065. }
  1066.  
  1067. void _VectorMA( const vec3_t veca, float scale, const vec3_t vecb, vec3_t vecc) {
  1068.     vecc[0] = veca[0] + scale*vecb[0];
  1069.     vecc[1] = veca[1] + scale*vecb[1];
  1070.     vecc[2] = veca[2] + scale*vecb[2];
  1071. }
  1072.  
  1073.  
  1074. vec_t _DotProduct( const vec3_t v1, const vec3_t v2 ) {
  1075.     return v1[0]*v2[0] + v1[1]*v2[1] + v1[2]*v2[2];
  1076. }
  1077.  
  1078. void _VectorSubtract( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  1079.     out[0] = veca[0]-vecb[0];
  1080.     out[1] = veca[1]-vecb[1];
  1081.     out[2] = veca[2]-vecb[2];
  1082. }
  1083.  
  1084. void _VectorAdd( const vec3_t veca, const vec3_t vecb, vec3_t out ) {
  1085.     out[0] = veca[0]+vecb[0];
  1086.     out[1] = veca[1]+vecb[1];
  1087.     out[2] = veca[2]+vecb[2];
  1088. }
  1089.  
  1090. void _VectorCopy( const vec3_t in, vec3_t out ) {
  1091.     out[0] = in[0];
  1092.     out[1] = in[1];
  1093.     out[2] = in[2];
  1094. }
  1095.  
  1096. void _VectorScale( const vec3_t in, vec_t scale, vec3_t out ) {
  1097.     out[0] = in[0]*scale;
  1098.     out[1] = in[1]*scale;
  1099.     out[2] = in[2]*scale;
  1100. }
  1101.  
  1102. void CrossProduct( const vec3_t v1, const vec3_t v2, vec3_t cross ) {
  1103.     cross[0] = v1[1]*v2[2] - v1[2]*v2[1];
  1104.     cross[1] = v1[2]*v2[0] - v1[0]*v2[2];
  1105.     cross[2] = v1[0]*v2[1] - v1[1]*v2[0];
  1106. }
  1107.  
  1108. vec_t VectorLength( const vec3_t v ) {
  1109.     return sqrt (v[0]*v[0] + v[1]*v[1] + v[2]*v[2]);
  1110. }
  1111.  
  1112.  
  1113. vec_t Distance( const vec3_t p1, const vec3_t p2 ) {
  1114.     vec3_t    v;
  1115.  
  1116.     VectorSubtract (p2, p1, v);
  1117.     return VectorLength( v );
  1118. }
  1119.  
  1120. vec_t DistanceSquared( const vec3_t p1, const vec3_t p2 ) {
  1121.     vec3_t    v;
  1122.  
  1123.     VectorSubtract (p2, p1, v);
  1124.     return v[0]*v[0] + v[1]*v[1] + v[2]*v[2];
  1125. }
  1126.  
  1127.  
  1128. void VectorInverse( vec3_t v ){
  1129.     v[0] = -v[0];
  1130.     v[1] = -v[1];
  1131.     v[2] = -v[2];
  1132. }
  1133.  
  1134. void Vector4Scale( const vec4_t in, vec_t scale, vec4_t out ) {
  1135.     out[0] = in[0]*scale;
  1136.     out[1] = in[1]*scale;
  1137.     out[2] = in[2]*scale;
  1138.     out[3] = in[3]*scale;
  1139. }
  1140.  
  1141.  
  1142. int Q_log2( int val ) {
  1143.     int answer;
  1144.  
  1145.     answer = 0;
  1146.     while ( ( val>>=1 ) != 0 ) {
  1147.         answer++;
  1148.     }
  1149.     return answer;
  1150. }
  1151.  
  1152.  
  1153.  
  1154. /*
  1155. =================
  1156. PlaneTypeForNormal
  1157. =================
  1158. */
  1159. int    PlaneTypeForNormal (vec3_t normal) {
  1160.     if ( normal[0] == 1.0 )
  1161.         return PLANE_X;
  1162.     if ( normal[1] == 1.0 )
  1163.         return PLANE_Y;
  1164.     if ( normal[2] == 1.0 )
  1165.         return PLANE_Z;
  1166.     
  1167.     return PLANE_NON_AXIAL;
  1168. }
  1169.  
  1170.  
  1171.  
  1172. /*
  1173. ================
  1174. MatrixMultiply
  1175. ================
  1176. */
  1177. void MatrixMultiply(float in1[3][3], float in2[3][3], float out[3][3]) {
  1178.     out[0][0] = in1[0][0] * in2[0][0] + in1[0][1] * in2[1][0] +
  1179.                 in1[0][2] * in2[2][0];
  1180.     out[0][1] = in1[0][0] * in2[0][1] + in1[0][1] * in2[1][1] +
  1181.                 in1[0][2] * in2[2][1];
  1182.     out[0][2] = in1[0][0] * in2[0][2] + in1[0][1] * in2[1][2] +
  1183.                 in1[0][2] * in2[2][2];
  1184.     out[1][0] = in1[1][0] * in2[0][0] + in1[1][1] * in2[1][0] +
  1185.                 in1[1][2] * in2[2][0];
  1186.     out[1][1] = in1[1][0] * in2[0][1] + in1[1][1] * in2[1][1] +
  1187.                 in1[1][2] * in2[2][1];
  1188.     out[1][2] = in1[1][0] * in2[0][2] + in1[1][1] * in2[1][2] +
  1189.                 in1[1][2] * in2[2][2];
  1190.     out[2][0] = in1[2][0] * in2[0][0] + in1[2][1] * in2[1][0] +
  1191.                 in1[2][2] * in2[2][0];
  1192.     out[2][1] = in1[2][0] * in2[0][1] + in1[2][1] * in2[1][1] +
  1193.                 in1[2][2] * in2[2][1];
  1194.     out[2][2] = in1[2][0] * in2[0][2] + in1[2][1] * in2[1][2] +
  1195.                 in1[2][2] * in2[2][2];
  1196. }
  1197.  
  1198.  
  1199. void AngleVectors( const vec3_t angles, vec3_t forward, vec3_t right, vec3_t up) {
  1200.     float        angle;
  1201.     static float        sr, sp, sy, cr, cp, cy;
  1202.     // static to help MS compiler fp bugs
  1203.  
  1204.     angle = angles[YAW] * (M_PI*2 / 360);
  1205.     sy = sin(angle);
  1206.     cy = cos(angle);
  1207.     angle = angles[PITCH] * (M_PI*2 / 360);
  1208.     sp = sin(angle);
  1209.     cp = cos(angle);
  1210.     angle = angles[ROLL] * (M_PI*2 / 360);
  1211.     sr = sin(angle);
  1212.     cr = cos(angle);
  1213.  
  1214.     if (forward)
  1215.     {
  1216.         forward[0] = cp*cy;
  1217.         forward[1] = cp*sy;
  1218.         forward[2] = -sp;
  1219.     }
  1220.     if (right)
  1221.     {
  1222.         right[0] = (-1*sr*sp*cy+-1*cr*-sy);
  1223.         right[1] = (-1*sr*sp*sy+-1*cr*cy);
  1224.         right[2] = -1*sr*cp;
  1225.     }
  1226.     if (up)
  1227.     {
  1228.         up[0] = (cr*sp*cy+-sr*-sy);
  1229.         up[1] = (cr*sp*sy+-sr*cy);
  1230.         up[2] = cr*cp;
  1231.     }
  1232. }
  1233.  
  1234. /*
  1235. ** assumes "src" is normalized
  1236. */
  1237. void PerpendicularVector( vec3_t dst, const vec3_t src )
  1238. {
  1239.     int    pos;
  1240.     int i;
  1241.     float minelem = 1.0F;
  1242.     vec3_t tempvec;
  1243.  
  1244.     /*
  1245.     ** find the smallest magnitude axially aligned vector
  1246.     */
  1247.     for ( pos = 0, i = 0; i < 3; i++ )
  1248.     {
  1249.         if ( fabs( src[i] ) < minelem )
  1250.         {
  1251.             pos = i;
  1252.             minelem = fabs( src[i] );
  1253.         }
  1254.     }
  1255.     tempvec[0] = tempvec[1] = tempvec[2] = 0.0F;
  1256.     tempvec[pos] = 1.0F;
  1257.  
  1258.     /*
  1259.     ** project the point onto the plane defined by src
  1260.     */
  1261.     ProjectPointOnPlane( dst, tempvec, src );
  1262.  
  1263.     /*
  1264.     ** normalize the result
  1265.     */
  1266.     VectorNormalize( dst );
  1267. }
  1268.  
  1269.  
  1270.